import { handleActions } from "redux-actions";
import {
    USERS_LOGIN,
    USERS_REG,
    GET_USERS,
    SET_CUR_PAGE,
    SET_EACH_PAGE,
    FIND_USERS,
    FIND_USER_GET

} from "../types";
import _ from 'lodash';
const InitData = {
    username: "z3",
    password: 123,
    phone: 13423423442,
}
const initData = {
    curPage: 1,//当前页
    eachPage: 10,//每页显示的条数
    total: 0,//总条数
    maxPage: 0,//总页数
    rows: []//当前页的数据
}
export const users = handleActions({
    [USERS_LOGIN]: (state, { payload }) => {
        return state
    },
    [USERS_REG]: (state, { payload }) => {
        return state
    },

}, InitData);
// 用户列表
export const usersData = handleActions({
    [GET_USERS]: (state, { payload }) => {
        const { total, maxPage, rows } = payload;
        return _.assign({}, state, {
            total, maxPage, rows
        })
    },
    [SET_CUR_PAGE]: (state, { payload }) => {
        const { curPage } = payload;
        const $state = _.clone(state);
        $state.curPage = curPage;
        return $state
    },
    [SET_EACH_PAGE]: (state, { payload }) => {
        const { eachPage } = payload;
        const $state = _.clone(state);
        $state.eachPage = eachPage;
        $state.rows = [];
        return $state
    },
    [FIND_USERS]: (state, { payload }) => {
        const { username, password, address, _id, phone } = payload.data;
        return _.assign({}, state, {
            username, password, address, _id, phone
        })
    },
    [FIND_USER_GET]: (state, { payload }) => {
        const { data } = payload
        // 查找拿到data跟total
        return _.assign({}, state, { rows: data, total: data.length })
    }
}, initData)

